package com.yhyvr.console.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lz.kit.LogKit;
import com.lz.kit.PropKit;
import com.lz.mybatis.jdbc.auto.dao.BoGoodsMapper;
import com.lz.mybatis.jdbc.auto.model.BoGoods;
import com.lz.mybatis.jdbc.auto.model.BoGoodsExample;
import com.lz.tool.LzStringUtils;
import com.lz.tool.ReadUrlTool;
import com.lz.tool.UUIDTool;
import com.lz.tool.aliupload.MediaUploadClient;
import com.yhyvr.console.annotation.RepeatSubmit;
import com.yhyvr.console.service.NewsHtmlReplaceTool;
import com.yhyvr.service.UploadService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yhyvr.console.model.common.ConsoleContext;
import com.yhyvr.pojo.req.console.BootStrapGridReq;
import com.yhyvr.pojo.resp.CommonResp;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.resp.console.BootStrapGridResp;

import javax.annotation.Resource;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * title:
 * </p>
 * <p>
 * description:
 * </p>
 *
 * @create 张钦
 * @update
 * @version 1.0
 * @date
 */
@Controller
public class BoGoodsController{
	@Resource
	private BoGoodsMapper modelMapper;
	@Resource
	private NewsHtmlReplaceTool newsHtmlReplaceTool;
	@Resource
	private UploadService uploadService;
	@RequestMapping("/console/bo_goods/add")
	public String add(){
		return "console/boGoods/boGoods_form";
	}

	@RequestMapping("/console/bo_goods/edit")
	public String edit(ModelMap modelMap,String id){
			modelMap.put("boGoods", modelMapper.selectByPrimaryKey(id));
		String serverUrl = PropKit.use("three-app.properties").get("sys.serverUrl");
		LogKit.info("系统服务地址："+serverUrl);
		String detail = ReadUrlTool.readUrl(serverUrl+"/file/goods/detail/"+id+".txt?"+new Date().getTime());
		String help =  ReadUrlTool.readUrl(serverUrl+"/file/goods/help/"+id+".txt?"+new Date().getTime());
		modelMap.put("detail",detail);
		modelMap.put("help",help);
		return "console/boGoods/boGoods_form";
	}

	@RequestMapping("/console/bo_goods/list")
	public String list(){
		return "console/boGoods/boGoods_table";
	}

	@RequestMapping("/console/bo_goods/json")
	public @ResponseBody BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		BoGoodsExample boGoodsExample = new BoGoodsExample();
        ControllerQueryTool.setBoGoodsCriteria(bootGridReq.getQuery(),boGoodsExample.createCriteria());
		List<BoGoods> boGoodsList = modelMapper.selectByExample(boGoodsExample);
		return new BootStrapGridResp(page.getTotal(),boGoodsList);
	}

	@RequestMapping("/console/bo_goods/save")
	public @ResponseBody HttpResult save(String formObjectJson, ConsoleContext consoleContext,String detail,String help){
		try{
			System.out.println(detail);
		BoGoods boGoods= JSON.parseObject(formObjectJson,BoGoods.class);
			String id=boGoods.getGoodsId();
		if (id!=null) {
			boGoods.setUpdateTime(new Date());
			modelMapper.updateByPrimaryKeySelective(boGoods);
			uploadService.uploadToDb(newsHtmlReplaceTool.getAliYunImageHtml(detail),File.separator+"file"+ File.separator+"goods"+ File.separator+"detail",boGoods.getGoodsId()+".txt");
			uploadService.uploadToDb(newsHtmlReplaceTool.getAliYunImageHtml(help),File.separator+"file"+ File.separator+"goods"+ File.separator+"help",boGoods.getGoodsId()+".txt");
		}else{
			boGoods.setGoodsId(UUIDTool.getUUID());
			boGoods.setCreateTime(new Date());
			boGoods.setUpdateTime(new Date());
			boGoods.setCreateUser(consoleContext.getUserId());
			modelMapper.insertSelective(boGoods);
			uploadService.uploadToDb(newsHtmlReplaceTool.getAliYunImageHtml(detail),File.separator+"file"+ File.separator+"goods"+ File.separator+"detail",boGoods.getGoodsId()+".txt");
			uploadService.uploadToDb(newsHtmlReplaceTool.getAliYunImageHtml(help),File.separator+"file"+ File.separator+"goods"+ File.separator+"help",boGoods.getGoodsId()+".txt");

		}
			return CommonResp.getSuccessByData(boGoods.getGoodsId());
		}catch(Exception e){
			LogKit.error(e.getMessage(),e);
			return CommonResp.getError();
		}

	}

	@RequestMapping("/console/bo_goods/delete")
	public @ResponseBody HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
			BoGoods boGoods = modelMapper.selectByPrimaryKey(String.valueOf(id));
			boGoods.setState(0);
		}
		return CommonResp.getSuccess();
	}
}
